<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Booking extends MY_Controller {

	function __construct() {
		parent::__construct();
		$this->load->model('u_user', 'user_m');
	}

	function select_flights_block() {
		$this->load->model('f_flight', 'flight_m');
		
		$data['booking_data'] = get_json_cookie('booking_box');
		$data['direction'] = $this->input->post('direction');
		$flights_data = $this->flight_m->get_flights_by_booking_data($data['booking_data'], $data['direction']);
		
		$data['flights_data'] = $this->common->process_flights_data($flights_data);
		
		//echo '<pre>';
		//print_r($flights_data);
		
		$this->load->view('module/index/select_flights_block', $data);
	}
	
	function passenger_details_block() {
		$this->load->model('f_flight', 'flight_m');
		$this->load->model('f_flight_scheduler', 'flight_scheduler_m');
		$this->load->model('const_airport', 'airport_m');
		$this->load->model('const_area', 'area_m');
		
		$data['booking_data'] = get_json_cookie('booking_box');
		$select_flights = get_json_cookie('select_flights');
		
		$i = 0;
		foreach ($select_flights as $v) {
			if (!empty($v)) {
				$flight = $this->flight_m->get_by_id($v);
				$flight_scheduler = $this->flight_scheduler_m->get_by_id($flight['flight_scheduler_id']);
					
				$airport['from'] = $this->airport_m->get_by_id($flight_scheduler['from_airport_id']);
				$airport['from']['area'] = $this->area_m->get_by_id($airport['from']['area_id']);
					
				$airport['to'] = $this->airport_m->get_by_id($flight_scheduler['to_airport_id']);
				$airport['to']['area'] = $this->area_m->get_by_id($airport['to']['area_id']);
					
				$data['selected_itinerary'][$i] = $flight;
				$data['selected_itinerary'][$i]['scheduler'] = $flight_scheduler;
				$data['selected_itinerary'][$i]['airport'] = $airport;
					
				$i ++;
			}
		}
		
		/*echo '<pre>';
		print_r($data);
		return ;*/
		$this->load->view('module/index/passenger_details_block', $data);
	}
	
	function get_customer_passenger_json() {
		$this->load->model('u_customer_passenger', 'customer_passenger_m');
	
		$passenger_data = $this->customer_passenger_m->get_customer_passenger_by_name_like($_SESSION['uid'], $this->input->get('term'), get_cookie('current_passenger_type'));
	
		$data = $passenger_data;
		foreach ($passenger_data as $k => $v) {
			$data[$k]['value'] = $v['passenger_name'];
		}
	
		//echo '<pre>';
		//print_r($passenger_data);
		$this->output->set_output(json_encode($data));
	}
	
	function finish_block() {
		$this->load->model('f_flight', 'flight_m');
		$this->load->model('f_flight_scheduler', 'flight_scheduler_m');
		$this->load->model('const_airport', 'airport_m');
		$this->load->model('const_area', 'area_m');
		
		$data['booking_data'] = get_json_cookie('booking_box');
		$select_flights = get_json_cookie('select_flights');
		
		$i = 0;
		foreach ($select_flights as $v) {
			if (!empty($v)) {
				$flight = $this->flight_m->get_by_id($v);
				$flight_scheduler = $this->flight_scheduler_m->get_by_id($flight['flight_scheduler_id']);
					
				$airport['from'] = $this->airport_m->get_by_id($flight_scheduler['from_airport_id']);
				$airport['from']['area'] = $this->area_m->get_by_id($airport['from']['area_id']);
					
				$airport['to'] = $this->airport_m->get_by_id($flight_scheduler['to_airport_id']);
				$airport['to']['area'] = $this->area_m->get_by_id($airport['to']['area_id']);
					
				$data['selected_itinerary'][$i] = $flight;
				$data['selected_itinerary'][$i]['scheduler'] = $flight_scheduler;
				$data['selected_itinerary'][$i]['airport'] = $airport;
					
				$i ++;
			}
		}
		
		$data['order'] = $this->get_order_details_by_passenger_ids_and_select_flights();
		
		/*echo '<pre>';
		print_r($data);
		return ;*/
		$this->load->view('module/index/finish_block', $data);
	}
	
	function get_order_details_by_passenger_ids_and_select_flights($passenger_ids = '') {
		$this->load->model('f_flight', 'flight_m');
		$this->load->model('u_customer_passenger', 'customer_passenger_m');
		
		$booking_data = get_json_cookie('booking_box');
		$select_flights = get_json_cookie('select_flights');
		$passenger_ids = get_json_cookie('passenger_ids');
		
		foreach ($select_flights as $flight_id) {
			if (!empty($flight_id)) {
				$flights_data[] = $this->flight_m->get_flight_data_by_id($flight_id);
			}
		}
		
		$process_flights_data = $this->common->process_flights_data($flights_data);
		
		$total_payment_amount = 0;
		foreach ($passenger_ids as $k => $passenger_id) {
			$passenger_data[$k] = $this->customer_passenger_m->get_by_id($passenger_id);
			
			if ($passenger_data[$k]['passenger_type'] == 'A') {
				$passenger_data[$k]['individual_discount'] = '0';
			} elseif ($passenger_data[$k]['passenger_type'] == 'C') {
				$passenger_data[$k]['individual_discount'] = '25';
			} else {
				$passenger_data[$k]['individual_discount'] = '100';
			}
			
			$passenger_data[$k]['route'] = $process_flights_data;
			
			foreach ($passenger_data[$k]['route'] as $k1 => $v) {
				$passenger_data[$k]['route'][$k1]['payment_amount'] = $passenger_data[$k]['route'][$k1]['fare'] * (100 - $passenger_data[$k]['individual_discount']) * 0.01;
				$total_payment_amount = $total_payment_amount + $passenger_data[$k]['route'][$k1]['payment_amount'];
			}
		}
		
		//echo '<pre>';
		//print_r($passenger_data);
		$data['passenger_data'] = $passenger_data;
		$data['total_payment_amount'] = $total_payment_amount;
		$data['itinerary_type'] = $booking_data['itinerary_type'];
		return $data;
	}
	
	function save_order() {
		$this->load->model('o_order', 'order_m');
		$this->load->model('link_order_passenger', 'order_passenger_m');
		$this->load->model('f_flight', 'flight_m');
		
		$booking_data = get_json_cookie('booking_box');
		$select_flights = get_json_cookie('select_flights');
		$passenger_ids = get_json_cookie('passenger_ids');
		
		foreach ($select_flights as $flight_id) {
			if (!empty($flight_id)) {
				$order_data['user_id'] = $_SESSION['uid'];
				$order_data['flight_id'] = $flight_id;
				$order_data['class'] = $booking_data['cabin_class'];
				$order_data['order_datetime'] = date('Y-m-d H:i:s');
				
				$order_id = $this->order_m->save($order_data);
				
				foreach ($passenger_ids as $passenger_id) {
					$order_passenger_data['order_id'] = $order_id;
					$order_passenger_data['passenger_id'] = $passenger_id;
				
					$this->order_passenger_m->save($order_passenger_data);
				}
					
				$flight_data = $this->flight_m->get_by_id($flight_id);
					
				if ($booking_data['cabin_class'] == 'Y') {
					$cabin_class_name = 'economy_class';
				} else {
					$cabin_class_name = 'prestige_class';
				}
					
				$update_flight_data['id'] = $flight_id;
				$update_flight_data[$cabin_class_name . '_remain_seats'] = $flight_data[$cabin_class_name . '_remain_seats'] - $booking_data['adult_count'] - $booking_data['child_count'];
				$this->flight_m->update($update_flight_data);
			}
		}
		
		$this->output->set_output('succeed');
	}
}